using Pdf2epub.BusinessLogic;
using Pdf2epub.Interfaces;
using System.Collections.Specialized;
using System.ComponentModel;

namespace Pdf2epub.Model
{
    public class ProjectContext : IProjectContext
    {
        public ProjectContext(IControler controler)
        {
            Controler = controler;
            ProjectConfiguration = new ProjectConfiguration();
            Content = new ContentBlockCollection();
            BulkService = new BulkService();

            Content.SetProjectContext(this);

            ConnectChangedEvents();
        }

        public ProjectContext(IControler controler, ProjectConfiguration configuration)
        {
            Controler = controler;
            ProjectConfiguration = configuration;
            Content = new ContentBlockCollection();
            BulkService = new BulkService();

            ProjectConfiguration.SetProjectContext(this);
            Content.SetProjectContext(this);

            ConnectChangedEvents();
        }

        public IBulkService BulkService { get; set; }

        public ContentBlockCollection Content { get; private set; }

        public IControler Controler { get; private set; }

        public IFeedbackService FeedbackService { get; set; }

        public bool IsUnsaved { get; private set; }

        public ProjectConfiguration ProjectConfiguration { get; private set; }

        public void ResetSavedState()
        {
            IsUnsaved = false;
        }

        private void ConnectChangedEvents()
        {
            Content.CollectionChanged += MarkContentChange;
            ProjectConfiguration.PropertyChanged += MarkContentChange;
        }

        private void MarkContentChange(object sender, PropertyChangedEventArgs e)
        {
            IsUnsaved = true;
        }

        private void MarkContentChange(object sender, NotifyCollectionChangedEventArgs e)
        {
            IsUnsaved = true;
        }
    }
}